import Koa from 'koa';

// 配置
import config from '../../config'

// 中间件
import middlewares from './middlewares/index'; // 自定义的中间件
import views from 'koa-views';
import json from 'koa-json';
import onerror from 'koa-onerror';
import bodyparser from 'koa-bodyparser';
import logger from 'koa-logger';
import favicon from 'koa-favicon';

/**
 * 构建服务器
 */
const app = new Koa();

onerror(app);

app.use(favicon(`./src/assets/favicon.ico`));

app.use(bodyparser({
    enableTypes: ['json', 'form', 'text']
}));

app.use(json());

app.use(logger());

app.use(require('koa-static')(`./dist/client`));

app.use(views('./dist/server',{extension:'ejs'}));

app.use(middlewares.render);

app.on('error', (err) => {
    console.error('server error', err);
})

app.listen(config.port, () => {
    console.log('[MY SSR] server started at port ' + config.port);
})